﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Development.DbData;
using System.Reflection;
using System.Threading;

namespace Development.DataEntity
{
    /// <summary>
    /// 数据基类
    /// </summary>
    public class DataEntityBase : IComparable
    {
        /// <summary>
        /// 锁
        /// </summary>
        private readonly object _lockFlag = new object();
        /// <summary>
        /// 数据块id 唯一确定一项数据
        /// </summary>
        [DbData(true)]
        public long ID { get; set; }

        /// <summary>
        /// 上次使用时间，用来判断过期
        /// </summary>
        public DateTime UseTime;

        /// <summary>
        /// 名次 用来排序用的
        /// </summary>
        public int Rank;

        public virtual int CompareTo(object obj)
        {
            return 0;
        }

        /// <summary>
        /// 获取数据库名
        /// </summary>
        /// <returns></returns>
        public string GetDbName()
        {
            DbDataAttribute attr = Attribute.GetCustomAttribute(this.GetType(), typeof(DbDataAttribute)) as DbDataAttribute;
            if (attr != null)
            {
                return attr.DbName;
            }
            return null;
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="action"></param>
        public void ModifyLock<T>(Action<T> action) where T : DataEntityBase
        {
            if (action != null)
            {
                if (action != null)
                {
                    try
                    {
                        Monitor.Enter(_lockFlag);
                        action(this as T);
                    }
                    finally
                    {
                        Monitor.Exit(_lockFlag);
                    }
                }
            }
        }

        /// <summary>
        /// 获取redis key
        /// </summary>
        /// <returns></returns>
        public string GetRedisKey()
        {
            return this.GetType().Name + "_" + ID;
        }

        public static string GetRedisKey<T>(long id)
        {
            return typeof(T).Name + "_" + id;
        }

        public static long GetKeyFromRedis(string redisKey)
        {
            int index = redisKey.IndexOf("_");
            string longKey = "0";
            if(index != -1)
            {
                longKey = redisKey.Remove(0,index + 1);
            }
            long id = 0;
            long.TryParse(longKey,out id);
            return id;
        }

        public static List<string> GetRedisKeys<T>(List<long> ids)
        {
            List<string> list = new List<string>();
            string tKey = typeof(T).Name;
            for(int i =0;i<ids.Count;++i)
            {
                list.Add(tKey + "_" + ids[i]);
            }
            return list;
        }

        public static string GetDbName<T>()
        {
            DbDataAttribute attr = Attribute.GetCustomAttribute(typeof(T), typeof(DbDataAttribute)) as DbDataAttribute;
            if (attr != null)
            {
                return attr.DbName;
            }
            return null;
        }
    }
}
